package surf.browser.component;

import java.awt.Color;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JWindow;

import surf.browser.TabManager;

/**
 * Allows for right-click popup menus
 * @author Tom Cashman
 *
 */
public class RightClickMenu extends JWindow {
	private static final long serialVersionUID = 1L;
	private String link;
	private TabManager manager;
	private JButton newTab, bookmark, copy;
	private RightClickMenu defaultInstance;

	/**
	 * Default constructor
	 * @param url
	 * @param m
	 * @param point
	 */
	public RightClickMenu(String url, TabManager m, Point point) {
		super();
		this.link = url;
		this.manager = m;
		this.getContentPane().setLayout(null);
		this.setBackground(Color.BLACK);
		this.getContentPane().setBackground(Color.BLACK);
		newTab = new JButton("Open in new tab");
		newTab.setSize(160, 24);
		newTab.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				manager.openNewTab(link);
				defaultInstance.dispose();
			}
		});
		newTab.setLocation(2, 2);
		this.getContentPane().add(newTab);
		
		copy = new JButton("Copy link");
		copy.setSize(160, 24);
		copy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
				final StringSelection s = new StringSelection(new String(link));
				clipboard.setContents(s, s);
				defaultInstance.dispose();
			}
		});
		copy.setLocation(2, 26);
		this.getContentPane().add(copy);
		
		bookmark = new JButton("Bookmark Link");
		bookmark.setSize(160, 24);
		bookmark.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//TODO: Add bookmark
				defaultInstance.dispose();
			}
		});
		bookmark.setLocation(2, 50);
		this.getContentPane().add(bookmark);
		
		this.setLocation(point);
		this.setSize(164, 76);
		this.setVisible(true);
		defaultInstance = this;
	}
}
